<?php
namespace app\controller;

use think\Request;
use app\model\Book as BookModel;
use app\common\UtilService;

class Book extends Base
{
	/**
	 * [index 首页]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function index(Request $request)
	{
		$param = UtilService::getMore([
            ['pre_id', ''],
            ['som_id', ''],
        ]);

		$nowList = BookModel::whereNull('delete_time')->field('id, title, create_time')->order('create_time DESC')->limit(3)->select();
		$hotList = BookModel::whereNull('delete_time')->field('id, title, create_time')->order('downloads_num DESC')->limit(9)->select();

		return view('index', compact('nowList', 'hotList', 'param'));
	}

	/**
	 * [detail 详细]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function detail(Request $request)
	{

		$param = UtilService::getMore([
            ['id', '1'],
        ]);

		$hotList = BookModel::whereNull('delete_time')->field('id, title, create_time')->order('downloads_num DESC')->limit(9)->select();

		$bookInfo = BookModel::whereNull('delete_time')->with(['pre', 'som'])->where('id', $param['id'])->find();
		// dump($bookInfo->toArray());exit;

		BookModel::where('id', $param['id'])->inc('yulan')->update();


		return view('detail', compact('hotList', 'bookInfo'));
	}

	/**
	 * [ajaxBookList 获取列表]
	 * @since  2022-04-22
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function ajaxBookList(Request $request)
	{
		try {

			$param = UtilService::getMore([
	            ['page', '1'],
	            ['limit', '10'],
	            ['pre_id', ''],
	            ['som_id', ''],
	        ]);

	        $list = BookModel::getBookList($param);

			return success($list, '获取成功');
	        
        } catch (\Exception $e) {

            // 这是进行异常捕获
            return error($e->getMessage());
        }
	}
}